package com.atguigu.gmall.service.impl;

import com.atguigu.gmall.cache.ShopCache;
import com.atguigu.gmall.client.service.SearchFeignClientService;
import com.atguigu.gmall.constant.MqConst;
import com.atguigu.gmall.entity.SkuImage;
import com.atguigu.gmall.entity.SkuInfo;
import com.atguigu.gmall.entity.SkuPlatformPropertyValue;
import com.atguigu.gmall.entity.SkuSalePropertyValue;
import com.atguigu.gmall.mapper.SkuInfoMapper;
import com.atguigu.gmall.service.SkuImageService;
import com.atguigu.gmall.service.SkuInfoService;
import com.atguigu.gmall.service.SkuPlatformPropertyValueService;
import com.atguigu.gmall.service.SkuSalePropertyValueService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rabbitmq.client.MessageProperties;
import org.csource.common.MyException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@Transactional
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoMapper, SkuInfo> implements SkuInfoService {

    @Autowired
    private SearchFeignClientService searchFeignClientService;
    @Autowired
    private SkuPlatformPropertyValueService skuPlatformPropertyValueService;
    @Autowired
    private SkuSalePropertyValueService skuSalePropertyValueService;
    @Autowired
    private SkuImageService skuImageService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @ShopCache(prefix = "pageCache")
    @Override
    public IPage selectPage(IPage<SkuInfo> skuInfoPage) {
        //查找sku信息
        IPage<SkuInfo> skuInfoIPage = baseMapper.selectPage(skuInfoPage, null);
        List<SkuInfo> skuInfos = skuInfoIPage.getRecords();
        //封装参数
        skuInfos.stream().forEach(item->{
            Long id = item.getId();//skuinfo的id
            packageResultValue(item,id);
        });
        return skuInfoIPage;
    }

    @ShopCache(prefix = "skuInfo")
    @Override
    public SkuInfo getById(Long skuId) {
        SkuInfo skuInfo = baseMapper.selectById(skuId);
        packageResultValue(skuInfo, skuId);
        return skuInfo;
    }

    @Override
    public void saveAll(SkuInfo skuInfo) {
        baseMapper.insert(skuInfo);
        if (!CollectionUtils.isEmpty(skuInfo.getSkuImageList())){
            //sku image
            skuImageService.saveBatch(skuInfo.getSkuImageList());
        }
        if (CollectionUtils.isEmpty(skuInfo.getSkuPlatformPropertyValueList())){
            //平台属性
            skuPlatformPropertyValueService.saveBatch(skuInfo.getSkuPlatformPropertyValueList());
        }
        if (CollectionUtils.isEmpty(skuInfo.getSkuSalePropertyValueList())){
            skuSalePropertyValueService.saveBatch(skuInfo.getSkuSalePropertyValueList());
        }
    }

    @Override
    public void onOrOffSaleBySkuId(Long skuId,Integer isSale) throws MyException {
        SkuInfo skuInfo = baseMapper.selectById(skuId);
        if (skuInfo == null)
            throw new MyException("无商品的sku信息");
        skuInfo.setIsSale(isSale);//设置上架或下架
        if (isSale == 1){
            //原始方式
//            searchFeignClientService.onSale(skuId);
            //使用消息队列来实现
            rabbitTemplate.convertAndSend(MqConst.ON_OFF_SALE_EXCHANGE, MqConst.ON_SALE_ROUTING_KEY,skuId);
        }else {
//            searchFeignClientService.offSale(skuId);
            rabbitTemplate.convertAndSend(MqConst.ON_OFF_SALE_EXCHANGE, MqConst.OFF_SALE_ROUTING_KEY, skuId);
        }
        baseMapper.updateById(skuInfo);  //修改
    }

    private void packageResultValue(SkuInfo skuInfo, Long id) {
        //平台属性
        QueryWrapper<SkuPlatformPropertyValue> queryWrapperPv = new QueryWrapper<>();
        queryWrapperPv.eq("sku_id", id);
        List<SkuPlatformPropertyValue> skuPlatformPropertyValues = skuPlatformPropertyValueService.list(queryWrapperPv);
        skuInfo.setSkuPlatformPropertyValueList(skuPlatformPropertyValues);

        //销售属性
        QueryWrapper<SkuSalePropertyValue> queryWrapperSv = new QueryWrapper<>();
//        Long productId = skuInfo.getProductId();
        queryWrapperSv.eq("sku_id", id);
        List<SkuSalePropertyValue> skuSalePropertyValues = skuSalePropertyValueService.list(queryWrapperSv);
        skuInfo.setSkuSalePropertyValueList(skuSalePropertyValues);

        //图片信息
        QueryWrapper<SkuImage> queryWrapperSi = new QueryWrapper<>();
        queryWrapperSi.eq("sku_id",id);
        List<SkuImage> skuImages = skuImageService.list(queryWrapperSi);
        skuInfo.setSkuImageList(skuImages);

    }

}
